package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Anonymous;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.model.LoginUser;
import com.yanfan.framework.web.service.TokenService;
import com.yanfan.zutai.domain.BDeviceZt;
import com.yanfan.zutai.mapper.BCommonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ignoreToken")
public class ZutaiIgnoreTokenController {
    /**
     * 获取设备管理详细信息，-2已过期，-1没有设置有效期，0以上的值表示是这个key的剩余有效时间
     */
    @Autowired
    private BCommonMapper commonMapper;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private TokenService tokenService;

    /**
     * 1 登录获取token
     *
     * @param ztGuid
     * @return
     */
    @GetMapping(value = "/getTokenByImei")
    public AjaxResult getTokenByImei(String ztGuid) {
        String token = "";
        BDeviceZt queryDeviceZt = new BDeviceZt();
        queryDeviceZt.setGuid(ztGuid);
        List<BDeviceZt> bDeviceZts = commonMapper.selectBDeviceZtList(queryDeviceZt);
        BDeviceZt bDeviceZt = null;
        if (bDeviceZts.size() > 0) {
            bDeviceZt = bDeviceZts.get(0);
            LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(bDeviceZt.getCreateBy());
            token = tokenService.createToken(loginUser);
        }
        return AjaxResult.success("token获取成功", token);
    }
}
